//
//  ValidatedType.swift
//  newshaohuo
//
//  Created by hujinbiao on 2018/3/27.
//  Copyright © 2018年 shaohuo. All rights reserved.
//
//正则表达式
import UIKit

class ValidatedType: NSObject {
    
    enum ValidatedType {
        
        case Email
        
        case PhoneNumber
        
        case IDCard
        
    }
    
    class func ValidateText(validatedType type: ValidatedType, validateString: String) -> Bool {
        
//        do {
        
            let pattern: String
            
            if type == ValidatedType.Email {
                
//                pattern = "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$"
                pattern = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
                
            }
                
            else if type == ValidatedType.PhoneNumber {
                
                pattern = "^1[0-9]{10}$"
//                pattern = "^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$"
                
            }else {
                
                pattern = "^(\\d{14}|\\d{17})(\\d|[xX])$"
            }

            let patternTest:NSPredicate = NSPredicate(format: "SELF MATCHES %@", pattern)
            
            return patternTest.evaluate(with: validateString)
            
            
//            let regex: NSRegularExpression = try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.caseInsensitive)
//
//            let matches = regex.matches(in: validateString, options: NSRegularExpression.MatchingOptions.reportProgress, range: NSMakeRange(0, validateString.characters.count))
//
//            return matches.count > 0
//
//        }
//
//        catch {
//
//            return false
//
//        }
//
    }
    
    class func EmailIsValidated(vStr: String) -> Bool {
        
        return ValidateText(validatedType: ValidatedType.Email, validateString: vStr)
        
    }
    
    class func PhoneNumberIsValidated(vStr: String) -> Bool {
        
        return ValidateText(validatedType: ValidatedType.PhoneNumber, validateString: vStr)
        
    }
    
    class func IDCardIsValidated(vStr: String) -> Bool {
        
        return ValidateText(validatedType: ValidatedType.IDCard, validateString: vStr)
        
    }

}
